import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Man extends Actor
{
    private int x;                          //Get the position of 'x' on screen.  
    private int y;                          //Get the position of 'y' on screen.
    private int y1;
    private int level;
    private int speed1;
    private int height;                     //Maximum height when jumping.
    private int initialHeight;              //Minimum height.
    private int pictNumber;                 //Index of the current picture.
    private GreenfootImage[] pictures;      //Man's pictures with or without jump shoes.

    /**
     * Man Constructor.
     */
    public Man(int lev)
    {
        level=lev;
        speed1 = -1;
        height = 90;
        initialHeight = 374;
        pictNumber = 0;
        pictures = new GreenfootImage[2];                   //Array with the pictures.
        pictures[0] = new GreenfootImage( "Man_01.gif");    //With jump shoes.
        pictures[1] = new GreenfootImage( "Man_02.gif");    //Without jump shoes.
        setImage( pictures[ pictNumber]);
    }

    /**
     * Method act.
     */
    public void act() 
    {
        int top = 0; 
        Actor tree = getOneIntersectingObject( Tree.class);
        Actor m = getOneIntersectingObject( Mountain.class);
        jump( (Tree)tree, (Mountain)m);
        if( isTouching( JumpShoes.class) == true )
        {
            pictNumber = 1;
            setImage( pictures[ pictNumber]);
            height = height + 10;
        }
    }    

    /**
     * Method jump simulates when the man jumps.
     */
    public void jump( Tree tree, Mountain m)
    {      
        x = super.getX();
        y = super.getY(); 
        int top=0, top2=0;
        boolean bird = isTouching(Bird.class);

        if( tree != null)
            top= tree.top();
        if( m != null)
            top2= m.top();
        if( bird == false)                  //The man can jump while he isn't touching the bird.
        {
            if( Greenfoot.isKeyDown( "up") && speed1 == -1 ){     //if press the up arrow...&& (tree == null || (tree != null && y<=top))
                speed1 = height;}
            if( speed1 > 0 )
            {
                setLocation( x, y - 4);
                speed1 = speed1 - 4;
            }
            else 
            if( (tree == null || (tree != null && y > top))&& ( m == null || (m != null && y > top2)) && y < initialHeight)
            {//if he can fall...
                setLocation( x, y + 4);
            }
            else
                speed1 = -1;
        }
    }

    /**
     * The following methods are flags that indicate if the man is touching an object.
     */
    public boolean toxic(Poison pX)
    {
        return intersects( pX);
    }

    public boolean food(Apple aX)
    {
        return intersects( aX);
    }

    public boolean wear(JumpShoes jsX)
    {
        return intersects( jsX);
    }

    public boolean finish( Mountain mX)
    {
        return intersects( mX);
    }
}